
<template>
  <div class="flow-header">
    <div class="info">
      <Icon @click="toWorkspace" type="md-arrow-back" size="22" style="margin-right: 10px;cursor:pointer" />
      <div>
        <div class="name">
          <span>{{flowInfo?.name}}</span>
          <Tooltip transfer theme="light" placement="top" max-width="400" :content="flowInfo?.description" v-if="flowInfo?.description">
            <Icon type="ios-alert-outline" size="16" />
          </Tooltip>
          <Icon type="md-create" style="cursor: pointer;margin-left: 10px" @click="handleEdit"></Icon>
        </div>
        <div class="save-time" v-show="saveTime[id]">已自动保存 {{saveTime[id]}}</div>
      </div>
    </div>
    <div class="action">
      <!-- <Icon type="md-time" size="16"  /> -->
      <Button type="primary" style="margin: 0 10px" @click="handlePublish">发布</Button>
    </div>
  </div>
  <div class="flow-content">
    <FlowCanvas :id="id" ref="flowRef" />
  </div>
  <EditDialog ref="editRef" @submit="editProjectSuccess" />
  <PublishDialog ref="publishRef" @submit="publishProjectSuccess" />
</template>
<script setup>
import { ref, watch, onMounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { flowStore } from '@/store/flow';
import { navTabStore } from '@/store/navTab';
import FlowCanvas from '@/components/FlowCanvas/index.vue'
import EditDialog from './components/EditDialog.vue';
import PublishDialog from './components/PublishDialog.vue';

import { detailFlow } from '@/api/modules/flow';

const route = useRoute();
const router = useRouter();
const {saveTime, setEditFlowID} = flowStore();
const {delVisitedRoute} = navTabStore();
// 工作流ID
const id = ref(undefined);
const flowRef = ref(null);
const flowInfo = ref(undefined)
const getFlowDetail = async() => {
  const { data } = await detailFlow({ data: { id: id.value } });
  flowInfo.value = data
}

const editRef = ref(null);
const handleEdit = () => {
  editRef.value?.init(flowInfo.value);
};
const editProjectSuccess = (data) => {
  flowInfo.value.name = data.name;
  flowInfo.value.description = data.description;
};

const publishRef = ref(null);
const handlePublish = () => {
  publishRef.value?.init(flowInfo.value);
};

const toWorkspace = () => {
  router.replace('/workspace');
  delVisitedRoute(route.path);
}

const publishProjectSuccess = () => {
  setTimeout(() => {
    toWorkspace()
  }, 500)
};

watch(() => route.query.id, (newId, oldId) => {
  id.value = newId;
  flowRef.value?.init(newId);
  setEditFlowID(id.value)
  getFlowDetail()
})

onMounted(() => {
  id.value = route.query.id;
  flowRef.value?.init(id.value);
  setEditFlowID(id.value)
  getFlowDetail()
})
</script>


<style lang="scss" scoped>
.flow-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 20px;
  width: 100%;
  background: #fff;
  border-bottom: #eee;

  .info {
    display: flex;
  }
  .name {
    font-weight: bold;
    color: #000;
    font-size: 14px;
    margin-bottom: 4px;
  }
  .save-time {
    padding: 0 4px;
    border-radius: 4px;
    font-size: 12px;
    margin-right: 4px;
    color: #999;
    background-color: rgba(87, 104, 161, 0.08);
    width: max-content;
  }

  .action {
    display: flex;
    align-items: center;
  }
}

.flow-content {
  width: 100%;
  height: calc(100% - 64px);
}
</style>
